package org.jeik.data.services;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jeik.data.dao.DAO;
import org.jeik.data.dao.DAOFactory;
import org.jeik.data.dao.DaoRecord;
import org.jeik.data.dao.DataException;
import org.jeik.data.models.Page;


/**
 * 
 * 
 * @author AAsha
 * 
 */
public class PageContentsService extends ServiceBase {
	
	
	/**
	 * 
	 */
	private PageContentsService(){
	}
	
	/**
	 * 
	 * @author AAsha
	 *
	 */
	private static class SingletonHolder{
		private static PageContentsService instance = new PageContentsService(); 
	}
	
	/**
	 * 
	 * @return PageContentsService instance
	 */
	public static PageContentsService getInstance(){
		return SingletonHolder.instance;
	}
	
	/**
	 * 
	 * @param page
	 * @throws DataException
	 */
	public void updatePage(Page page) throws DataException{
		
		DAO dao = DAOFactory.getInstance().createDao();
		
		Map<Integer, Object> params = new HashMap<Integer, Object>();
		params.put(1, page.getTitle());
		params.put(2, page.getBody());
		params.put(3, page.getId());
		
		List<String> keys = new ArrayList<String>();
		keys.add(Columns.ID.name());
		
		dao.update(page, keys, params);
	}
	
	
	/**
	 * 
	 * @param pageId
	 * @return Page
	 * @throws DataException
	 */
	public Page loadPage(Integer pageId) throws DataException{
		
		DAO dao = DAOFactory.getInstance().createDao();
		
		List<String> fields = new ArrayList<String>();
		fields.add(Columns.TITLE.name());
		fields.add(Columns.BODY.name());
		fields.add(Columns.PAGE_NAV.name());
		
		List<String> keys = new ArrayList<String>();
		keys.add(Columns.ID.name());
		
		Map<Integer, Object> params = new HashMap<Integer, Object>();
		params.put(1, pageId);
		
		dao.selectWhere(Tables.pages.name(), fields , keys, params);
		
		Page page = null;
		
		for(DaoRecord record:dao.getResult()){
			
			page = new Page();
			page.setTitle(record.getValue(Columns.TITLE.name()).toString());
			page.setBody(record.getValue(Columns.BODY.name()).toString());
			page.setPageNav(record.getValue(Columns.PAGE_NAV.name()).toString());
		}
		
		
		return page;
	}
	
	/**
	 * 
	 * @return content pages
	 * @throws DataException
	 */
	public Map<String,Page> loadContentPages() throws DataException{
		
		Map<String,Page> pages = new HashMap<String, Page>();
		
		DAO dao = DAOFactory.getInstance().createDao();
		
		List<String> fields = new ArrayList<String>();
		fields.add(Columns.ID.name());
		fields.add(Columns.TITLE.name());
		fields.add(Columns.BODY.name());
		fields.add(Columns.PAGE_NAV.name());

		dao.select(Tables.pages.name(), fields);
		
		Page page = null;
		
		for(DaoRecord record:dao.getResult()){
			
			page = new Page();
			page.setId(record.getValue(Columns.ID.name()).toString());
			page.setTitle(record.getValue(Columns.TITLE.name()).toString());
			page.setBody(record.getValue(Columns.BODY.name()).toString());
			page.setPageNav(record.getValue(Columns.PAGE_NAV.name()).toString());
			
			pages.put(page.getId(), page);
		}
		
		return pages;
	}

	
}
